package com.zhdl.modules.common.protocol_param.web.controller;

import com.project.network.entity.PortComm;
import com.project.network.entity.PortSocket;
import com.zhdl.common.util.WebResult;
import com.zhdl.modules.common.protocol_param.service.ProtocolParamService;
import com.zhdl.modules.common.protocol_param.web.request.ProtocolCommReq;
import com.zhdl.modules.common.protocol_param.web.request.ProtocolSocketReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(path = "/protocolParam", produces = MediaType.APPLICATION_JSON_VALUE)
public class ProtocolParamController {

    @Resource
    private ProtocolParamService protocolParamService;

    /**
     * 测试接口
     *
     * @return 测试服务是否启动
     */
    @GetMapping("/demo")
    public WebResult<String> InterfaceDebugging() {
        return WebResult.okResult("当前接口通讯正常");
    }

    /**
     * 新增串口
     *
     * @return /
     */
    @PostMapping("/protocol/insertPortComm")
    public WebResult<PortComm> insertPortComm(PortComm portComm) {
        PortComm portCommResult = protocolParamService.savePortCommDict(portComm);
        return WebResult.okResult(portCommResult);
    }

    /**
     * 更新串口
     *
     * @return /
     */
    @PostMapping("/protocol/updatePortComm")
    public WebResult<PortComm> updatePortComm(PortComm portComm) {
        PortComm portCommResult = protocolParamService.updatePortCommDict(portComm);
        return WebResult.okResult(portCommResult);
    }

    /**
     * 删除串口
     *
     * @param portCommId 串口Id
     * @return /
     */
    @GetMapping("/protocol/deletePortComm")
    public WebResult<String> deletePortComm(@RequestParam String portCommId) {
        protocolParamService.deletePortCommDict(portCommId);
        return WebResult.okResult("删除成功");
    }

    /**
     * 查询所有 串口
     */
    @GetMapping("/protocol/comm/findAllList")
    public WebResult<List<PortComm>> findAllCommList() {
        List<PortComm> allProtocolComm = protocolParamService.findAllProtocolComm();
        return WebResult.okResult(allProtocolComm);
    }

    /**
     * 分页查询串口列表
     *
     * @param request 查询条件
     * @return /
     */
    @PostMapping("/protocol/comm/findAllPage")
    public WebResult<Page<PortComm>> findAllCommByPage(@RequestBody ProtocolCommReq request) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createDate");
        Pageable pageable = PageRequest.of(request.getPageNumber(), request.getPageSize(), sort);
        Page<PortComm> allProtocolCommByCondition = protocolParamService.
                findAllProtocolCommByCondition(request.getQueryCriteria(), pageable);
        return WebResult.okResult(allProtocolCommByCondition);
    }

    /**
     * 新增网口
     *
     * @return /
     */
    @PostMapping("/protocol/insertPortSocket")
    public WebResult<PortSocket> insertPortSocket(PortSocket portSocket) {
        PortSocket portCommResult = protocolParamService.savePortSocketDict(portSocket);
        return WebResult.okResult(portCommResult);
    }

    /**
     * 更新网口
     *
     * @return /
     */
    @PostMapping("/protocol/updatePortSocket")
    public WebResult<PortSocket> updatePortSocket(PortSocket portSocket) {
        PortSocket portSocketResult = protocolParamService.updatePortSocketDict(portSocket);
        return WebResult.okResult(portSocketResult);
    }

    /**
     * 删除网口
     *
     * @param portSocketId 网口Id
     * @return /
     */
    @GetMapping("/protocol/deletePortSocket")
    public WebResult<String> deletePortSocket(@RequestParam String portSocketId) {
        protocolParamService.deletePortSocketDict(portSocketId);
        return WebResult.okResult("删除成功");
    }

    /**
     * 查询所有 网口
     */
    @GetMapping("/protocol/socket/findAllList")
    public WebResult<String> findAllSocketList() {
        protocolParamService.findAllProtocolSocket();
        return WebResult.okResult("删除成功");
    }

    /**
     * 分页查询串口列表
     *
     * @param request 查询条件
     * @return /
     */
    @PostMapping("/protocol/socket/findAllPage")
    public WebResult<Page<PortSocket>> findAllSocketByPage(@RequestBody ProtocolSocketReq request) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createDate");
        Pageable pageable = PageRequest.of(request.getPageNumber(), request.getPageSize(), sort);
        Page<PortSocket> allProtocolSocketByCondition = protocolParamService.
                findAllProtocolSocketByCondition(request.getQueryCriteria(), pageable);
        return WebResult.okResult(allProtocolSocketByCondition);
    }

}
